package com.yingxin.yxerp.module.admin;

import com.alibaba.fastjson.JSONObject;
import com.yingxin.yx.framework.commons.exception.BizException;
import com.yingxin.yx.framework.wx.WeixinAccessToken;
import com.yingxin.yx.framework.wx.WeixinSupport;
import com.yingxin.yxerp.module.admin.dto.BindWechatDto;
import com.yingxin.yxerp.module.admin.dto.BindWechatInfoDto;
import com.yingxin.yxerp.module.organization.service.IUserService;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

@RestController
@RequestMapping("/u/wx")
public class WexinAuthController {

    @Autowired
    private WeixinSupport weixinSupport;

    @Autowired
    private IUserService userService;

    /**
     * 测试用，实际中是由前端js发起
     */
    /*
    @ApiOperation(value = "授权登录：获取微信扫码地址")
    @RequestMapping(value = "/login/qrcode", method = RequestMethod.POST)
    @ResponseBody
    public String getLoginQRconnectUrl() throws UnsupportedEncodingException {
        String redirectUri = "http://zzx.natappvip.cc/erp/a/wechat/login";
        String oauthUrl = weixinSupport.getQRconnectUrl(redirectUri);
        return oauthUrl;
    }*/

    /**
     * 测试用，实际中是由前端js发起
     */
    /*@ApiOperation(value = "授权绑定：获取微信扫码地址")
    @RequestMapping(value = "/bind/qrcode", method = RequestMethod.POST)
    @ResponseBody
    public String getBindQRconnectUrl() throws UnsupportedEncodingException {
        String redirectUri = "http://zzx.natappvip.cc/erp/a/wechat/bind?u=" + "740663264806961152";
        String oauthUrl = weixinSupport.getQRconnectUrl(redirectUri);
        return oauthUrl;
    }*/

    @ApiOperation(value = "授权绑定：redirect_uri")
    @PostMapping("/bind")
    public void bindWechat(@RequestBody BindWechatDto dto) {
        try {
            WeixinAccessToken accessToken = weixinSupport.getAccessToken(dto.getCode(), null);
            String userInfo = weixinSupport.getUserInfo(accessToken.getOpenId(), accessToken.getAccessToken());
            JSONObject userInfoObject = JSONObject.parseObject(userInfo);
            String nickname = userInfoObject.getString("nickname");
            String url = userInfoObject.getString("avatarUrl");
            /*
            String user_openid = userInfoObject.getString("openid");
            String user_sex = userInfoObject.getString("sex");
            String user_province = userInfoObject.getString("province");
            String user_city = userInfoObject.getString("city");
            String user_country = userInfoObject.getString("country");
            String user_headimgurl = userInfoObject.getString("headimgurl");
            */
            userService.bindWechat(dto.getUserId(), accessToken.getOpenId(), nickname);
        } catch (Exception e) {
            throw new BizException("微信绑定失败");
        }

    }

    @ApiOperation(value = "授权绑定：redirect_uri")
    @PostMapping("/bindInfo")
    public void bindWechatInfo(@RequestBody BindWechatInfoDto dto) {
        try {
            userService.bindWechatInfo(dto.getUserId(), dto.getOpenId(), dto.getNickName(), dto.getWechatAvatarUrl());
        } catch (Exception e) {
            throw new BizException("微信绑定失败");
        }
    }

    @ApiOperation(value = "解除绑定：redirect_uri")
    @GetMapping("/unBindInfo/{userId}")
    public void unBindWechatInfo(@PathVariable("userId") String userId) {
        try {
            userService.wechatOnlineTranslation( userId );
        } catch (Exception e) {
            throw new BizException("微信绑定失败");
        }
    }
}
